/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ScannerParser;

import java.util.*;
/**
 *
 * @author jefoutta
 */
public class Temporal {

    private String name;
    private Character charValue;
    private Integer intValue;
    private String  stringValue;
    private Boolean booleanValue;
    private int index;
    private boolean isChar;
    private boolean isInt;
    private boolean isString;
    private boolean isBoolean;
    private static int count = 0;
    private static LinkedList<Temporal> available = new LinkedList();
    private static Temporal tcmp = new Temporal(true);

    public Temporal(){
        name = "t"+count;
        count++;
    }

    public Temporal(boolean b){
        name = "tcmp";
    }

    public void recicle(){
        available.add(this);
    }

    public char getCharValue(){
        return charValue.charValue();
    }

    public int getIntValue(){
        return intValue.intValue();
    }

    public String getStringValue(){
        return stringValue;
    }

    public boolean getBooleanValue(){
        return booleanValue.booleanValue();
    }

    public void setCharValue(char value){
        charValue = value;
    }

    public void setIntValue(int value){
        intValue = value;
    }

    public void setStringValue(String value){
        stringValue = value;
    }

    public void setBooleanValue(boolean value){
        booleanValue = value;
    }

    public String getName(){
        if(name.equals("tcmp"))
            return name;
        int offset = SymbolTable.actualSymbolTable.getWidth() + SymbolTable.actualSymbolTable.getIndexOfTemporal(this)*2;
        return name+"["+offset+"]";
    }

    public static Temporal getTemporal(){
        Temporal temp;
        int j = 0;
        if(available.size() > 0){
            while(j < available.size()){
                temp = available.get(j);
                if(SymbolTable.actualSymbolTable.containsTemporal(temp)){
                    temp = available.remove(j);
                    return temp;
                }
                j++;
            }
            temp = available.removeFirst();
            if(!SymbolTable.actualSymbolTable.containsTemporal(temp))
                SymbolTable.actualSymbolTable.addTemporal(temp);
            return temp;
        }else{
            temp = new Temporal();
            SymbolTable.actualSymbolTable.addTemporal(temp);
            return temp;
        }
    }

    public static Temporal getCMP(){
        return tcmp;
    }
}
